/*************************************************************************************
*                                  SRL ENGINE                                        *
*                                                                                    *
*                                                                                    *
*  File : Shader.cpp                                                             *
*  Author: SRL Killing                                                               *
*  Date: 25/02/10 20:30                                                              *
*  Description:                                                                      *
*                                                                                    *  
*  Licence :  LGPL                                                                   *
*                                                                                    *
*    Copyright (C) 2010 Pecatte Baptiste                                             *
*                                                                                    *
*    This program is free software; you can redistribute it and/or                   *
*    modify it under the terms of the GNU Lesser General Public                      *
*    License as published by the Free Software Foundation; either                    *
*    version 2.1 of the License, or (at your option) any later version.              *
*                                                                                    *
*    This program is distributed in the hope that it will be useful,                 *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of                  *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU                *
*    Lesser General Public License for more details.                                 *
*                                                                                    *
*    You should have received a copy of the GNU Lesser General Public                *
*    License along with this program; if not, write to the Free Software             *
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    *
*                                                                                    *
**************************************************************************************/

#include <string>
#include <fstream>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <SRL/Utils/Vector.h>
#include <SRL/Utils/Matrix.h>
#include <SRL/Utils/Extensions.h>
#include <SRL/Engine/Texture.h>
#include <SRL/Engine/FrameBuffer.h>
#include <SRL/Engine/Shader.h>
#include <SRL/Engine/Program.h>

namespace srl {

    Shader::Shader(GLenum type) {
        id = glCreateShaderObject(type);
    }
    Shader::Shader(GLenum type, std::string url) {
        id = glCreateShaderObject(type);
        SourceFromFile(url);
    }
    Shader::~Shader() {
        glDeleteObject(id);
    }
    
    
    void Shader::Source(std::string source) {
        const char *src = source.c_str();
        glShaderSource(id, 1, &src, NULL);
    }
    void Shader::SourceFromFile(std::string url) {
        std::string source;
        std::ifstream file(url.c_str());

        if(file){
            std::string line;
            while (std::getline(file, line)) {
                source += line;
                source += "\n";
            }
        }
        
        Source(source);
    }
    bool Shader::SourceAndCompile(std::string url) {
        SourceFromFile(url);
        return Compile();
    }
    
    bool Shader::Compile() {
        glCompileShader(id);
        GLint result;
        glGetShaderiv(id, GL_COMPILE_STATUS, &result);
        return (result==GL_TRUE) ? true : false;
    }
    std::string Shader::Log() {
        GLint logsize;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &logsize);
        char *log = NULL;
        log = (char*)malloc(logsize + 1);
        glGetShaderInfoLog(id, logsize, &logsize, log);
        return log;
    }
};
